package boundary;

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;

public class Menu implements IMenu {

	private Scanner menuScan;
	private String rm20;
	private int port;
	volatile private ReentrantLock lock;
	

	public Menu(Scanner menuScan, int port) {
		this.menuScan = menuScan;
		this.port=port;
		lock = new ReentrantLock();
	}
	
	public ReentrantLock getLock(){
		return lock;
	}

	/* (non-Javadoc)
	 * @see boundary.IMenu#showMenu()
	 */
	@Override
	public void showMenu(double tara ,double brutto, String p111, String rm201,String rm202, String rm203, String display, String debug1, String debug2){
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("******************* Display *********************");
		System.out.println(rm201);
		System.out.println(rm202+ " " + rm203+"                            "+display);
		System.out.println(p111);
		System.out.println("*************************************************");
		System.out.println();
		System.out.println("***************** Debug info ********************");
		System.out.println("Tilknyttet port: "+port);
		System.out.println("Brutto: "+brutto+" kg" );
		System.out.println("Tara: "+tara+" kg" );		
		System.out.println("Seneste modtagne kommando: " + debug1);
		System.out.println("Seneste afsendte svar: " + debug2);
		System.out.println("*************************************************");
		System.out.println();
		System.out.println("Indtast (T/B/Q for taratryk / brutto ændring / quit)");
		System.out.print ("Tast her: ");
		
	}
	
	@Override
	public void unConnected(String clientIP){
		System.out.println();
		System.out.println("Forbindelse til " + clientIP + " lukket.");
	}
	
	@Override
	public void connected(String clientIP){
		System.out.println();
		System.out.println("Tilsluttet til: " + clientIP);
	}
	
	@Override
	public double askDouble() {
		System.out.println("Indtast vægt: ");
		Double out;
		try {
			out = menuScan.nextDouble();
			if (out < 0){
				throw new InputMismatchException();
			}
			menuScan.nextLine();
		} catch (InputMismatchException e){
			menuScan.nextLine();
			System.out.println("Du skal indtaste et positivt hel- eller decimaltal, separeret med ',' :");
			out= askDouble();
		} 
		return out;
	}
	
	@Override
	public String getRM20() {
		while(rm20 == null){
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		String out = rm20;
		rm20 = null;
		return out;
	}

	@Override
	public void print(String txt) {
		System.out.println(txt);
	}

	@Override
	public String scan() {
		return menuScan.nextLine();
	}

	@Override
	public String showRM20(double tara, double brutto, String rm201, String rm202, String rm203, String debug1, String debug2) {
		showMenu(tara, brutto, "", rm201, rm202, rm203, "", debug1, debug2);		
		return getRM20();
	}

	@Override
	public void setRM20(String output) {
		rm20 = output;		
	}


	
}
